package cn.getech.data.development.service.impl;

import cn.getech.data.development.entity.DataExplore;
import cn.getech.data.development.mapper.DataExploreMapper;
import cn.getech.data.development.service.DataExploreService;
import cn.getech.data.intelligence.common.utils.PageUtils;
import cn.getech.data.intelligence.common.utils.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 数据探索 服务实现类
 * </p>
 *
 * @author zenith
 * @since 2019-07-08
 */
@Service
public class DataExploreServiceImpl extends ServiceImpl<DataExploreMapper, DataExplore> implements DataExploreService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String tableName = (String)params.get("tableName");
        Integer createPer = (Integer)params.get("createPer");
        String sql = (String)params.get("sql");
        //当前时间
        Calendar calendar=Calendar.getInstance();
//        Date date=calendar.getTime();
//        //三个月前
//        calendar.add(Calendar.MONTH,-3);
//        Date date1=calendar.getTime();

        IPage<DataExplore> page = this.page(
                new Query<DataExplore>().getPage(params),
                new QueryWrapper<DataExplore>().like(StringUtils.isNotBlank(tableName),"table_name", tableName)
                        .eq("create_per", createPer).like(StringUtils.isNotBlank(sql),"bdp_data_explore.sql", sql)
        );
        return new PageUtils(page);
    }

    @Override
    public Integer isTenantAdmin(Integer tenantId, Integer userId) {
        return this.baseMapper.isTenantAdmin(tenantId,userId);
    }

    @Override
    public Integer getSqlLimit(Integer tenantId, Integer userId) {
        return this.baseMapper.getSqlLimit(tenantId,userId);
    }

}
